package com.wenchao.demoworkmanager.works;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

/**
 * 【类的描述】介绍一下该类的用途
 *
 * @author zhangwenchao
 * @date 2021-12-17
 */
public class UploadLogsWork extends Worker {

    public static final String PROGRESS = "PROGRESS";

    public UploadLogsWork(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
        Data inputData = workerParams.getInputData();
        Log.d("super", "工作内容：" + inputData.getString("work_content"));
    }

    @NonNull
    @Override
    public Result doWork() {
        for (int i = 1; i < 11; i++) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.d("super", String.format("日志文件%d上传成功！", i));
            setProgressAsync(new Data.Builder().putInt(PROGRESS, i * 100 / 10).build());
        }
        return Result.success();
    }

}
